package io.gitee.zicai.bus;

import io.gitee.zicai.bus.adaptor.DefaultBusReceiveAdaptor;
import io.gitee.zicai.bus.adaptor.DefaultBusSendAdaptor;
import io.gitee.zicai.bus.adaptor.IBusReceiveAdaptor;
import io.gitee.zicai.bus.adaptor.IBusSendAdaptor;
import io.gitee.zicai.bus.bus.IReceiveBus;
import io.gitee.zicai.bus.bus.ISendBus;
import io.gitee.zicai.bus.config.BusConst;
import io.gitee.zicai.bus.core.IBusReceiver;
import io.gitee.zicai.bus.core.IBusSender;
import io.gitee.zicai.bus.listener.EventBusReceiveListener;
import io.gitee.zicai.bus.listener.EventBusSendListener;
import io.gitee.zicai.bus.listener.GuavaBusReceiveListener;
import io.gitee.zicai.bus.listener.GuavaBusSendListener;
import io.gitee.zicai.bus.listener.IBusReceiveListener;
import io.gitee.zicai.bus.listener.IBusSendListener;
import io.gitee.zicai.bus.publish.EventBusPublisher;
import io.gitee.zicai.bus.publish.GuavaBusPublisher;
import io.gitee.zicai.bus.publish.IBusPublisher;
import io.gitee.zicai.bus.support.GuavaEvent;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * BusAutoConfiguration
 *
 * @author zicai
 * @since 2021-01-18
 */
@Configuration
@ComponentScan("io.gitee.zicai.bus")
@EnableAsync
public class BusAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public <S extends IBusSender, SB extends ISendBus<S, ST>, ST> IBusSendAdaptor<S, SB, ST> defaultBusSendAdaptor() {
        return new DefaultBusSendAdaptor<>();
    }

    @Bean
    @ConditionalOnMissingBean
    public <R extends IBusReceiver, RB extends IReceiveBus<R, RT>, RT> IBusReceiveAdaptor<R, RB, RT> defaultBusReceiveAdaptor() {
        return new DefaultBusReceiveAdaptor<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_EVENT, matchIfMissing = true)
    public <S extends IBusSender, SB extends ISendBus<S, ST>, ST> IBusSendListener<S, SB, ST> busEventSendListener() {
        return new EventBusSendListener<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_EVENT, matchIfMissing = true)
    public <R extends IBusReceiver, RB extends IReceiveBus<R, RT>, RT> IBusReceiveListener<R, RB, RT> busEventReceiveListener() {
        return new EventBusReceiveListener<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_EVENT, matchIfMissing = true)
    public <B extends ApplicationEvent> IBusPublisher<B> cmdBusEventPublisher() {
        return new EventBusPublisher<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_GUAVA)
    public <S extends IBusSender, SB extends ISendBus<S, ST>, ST> IBusSendListener<S, SB, ST> cmdGuavaSendListener() {
        return new GuavaBusSendListener<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_GUAVA)
    public <R extends IBusReceiver, RB extends IReceiveBus<R, RT>, RT> IBusReceiveListener<R, RB, RT> cmdGuavaReceiveListener() {
        return new GuavaBusReceiveListener<>();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = BusConst.PREFIX, name = BusConst.TRIGGER_TYPE, havingValue = BusConst.TRIGGER_TYPE_GUAVA)
    public <B extends GuavaEvent> IBusPublisher<B> cmdBusGuavaPublisher() {
        return new GuavaBusPublisher<>();
    }

    /**
     * 异步线程池配置
     */
    @Bean("busAsyncPool")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(10);
        // 设置最大线程数
        executor.setMaxPoolSize(20);
        // 设置队列容量
        executor.setQueueCapacity(1000);
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("Bus-AsyncPool-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }
}
